﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Data;

namespace DataAccess.Classes
{
    public class HangHoa
    {
        #region Anh xa thuoc tinh
        private string _MaHangHoa;

        public string MaHangHoa
        {
            get { return _MaHangHoa; }
            set { _MaHangHoa = value; }
        }
        private string _TenHangHoa;

        public string TenHangHoa
        {
            get { return _TenHangHoa; }
            set { _TenHangHoa = value; }
        }
        private string _MaThuongHieu;

        public string MaThuongHieu
        {
            get { return _MaThuongHieu; }
            set { _MaThuongHieu = value; }
        }
        private int _GiaMua;

        public int GiaMua
        {
            get { return _GiaMua; }
            set { _GiaMua = value; }
        }
        private int? _GiaBan;

        public int? GiaBan
        {
            get { return _GiaBan; }
            set { _GiaBan = value; }
        }
        private int _ThoiGianBaoHanh;

        public int ThoiGianBaoHanh
        {
            get { return _ThoiGianBaoHanh; }
            set { _ThoiGianBaoHanh = value; }
        }

        private DateTime? _NgayBan;

        public DateTime? NgayBan
        {
            get { return _NgayBan; }
            set { _NgayBan = value; }
        }
        private DateTime? _NgayMua;

        public DateTime? NgayMua
        {
            get { return _NgayMua; }
            set { _NgayMua = value; }
        }

        private int _TrangThai;

        public int TrangThai
        {
            get { return _TrangThai; }
            set { _TrangThai = value; }
        }

        private string _MaGioHang;

        public string MaGioHang
        {
            get { return _MaGioHang; }
            set { _MaGioHang = value; }
        }

        private string _HinhAnh;

        public string HinhAnh
        {
            get { return _HinhAnh; }
            set { _HinhAnh = value; }
        }
        #endregion

        public HangHoa() { }

        public static HangHoa Single(string maHangHoa)
        {
            try
            {
                return CBO.FillObject<HangHoa>(DataProvider.Instance.ExecuteReader("HangHoa_ChonMa", maHangHoa));
            }
            catch 
            {
                return null;
                //throw;
            }
        }

        public static List<HangHoa> InCategory(string maThuongHieu, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try 
            { 
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("HangHoa_TheoThuongHieu", maThuongHieu, Convert.ToInt32(page), pageSize); 
                reader.Read(); 
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize); 
                reader.NextResult();
                return CBO.FillCollection<HangHoa>(reader);
            }
            catch 
            { 
                if (reader != null && reader.IsClosed == false) 
                    reader.Close(); 
                howManyPages = 0; 
                return new List<HangHoa>();
            }
        }

        public static List<HangHoa> All()
        {
            IDataReader reader = null;
            try
            {
                
                reader = DataProvider.Instance.ExecuteReader("HangHoa_All");
                
                return CBO.FillCollection<HangHoa>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                return new List<HangHoa>();
            }
        }

        public static List<HangHoa> OnPromo(string page, out int howManyPages)
        {
            IDataReader reader = null; 
            try
            { 
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("HangHoa_TheoTrang", page, GlobalConfiguration.PageSize); 
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult(); 
                List<HangHoa> hh = CBO.FillCollection<HangHoa>(reader);
                return hh;
            }
            catch 
            {
                if (reader != null && reader.IsClosed == false) 
                    reader.Close(); 
                howManyPages = 0; 
                return new List<HangHoa>();
            }
        }
        //
        public static bool Add(HangHoa hanghoa)
        {
            try
            {
                hanghoa.NgayMua = DateTime.Now;
                int rs = DataProvider.Instance.ExecuteNonQuery("HangHoa_Them",
                hanghoa.MaHangHoa, hanghoa.TenHangHoa, hanghoa.MaThuongHieu, hanghoa.GiaMua,
                hanghoa.GiaBan, hanghoa.ThoiGianBaoHanh, hanghoa.NgayBan, hanghoa.NgayMua, hanghoa.TrangThai, hanghoa.HinhAnh);
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Update(HangHoa hanghoa)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("HangHoa_Sua",
                hanghoa.MaHangHoa, hanghoa.TenHangHoa, hanghoa.MaThuongHieu, hanghoa.GiaMua,
                hanghoa.GiaBan, hanghoa.ThoiGianBaoHanh, hanghoa.NgayBan, hanghoa.NgayMua, hanghoa.TrangThai, hanghoa.HinhAnh);
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }

        public static List<HangHoa> Search(string searchString, int giaDuoi, int giaTren, string page, out int howManyPages) 
        {
            IDataReader reader = null;
            try 
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("HangHoa_TimKiem", searchString, giaDuoi, giaTren, page, GlobalConfiguration.PageSize);
                reader.Read(); 
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult(); 
                return CBO.FillCollection<HangHoa>(reader);
            } 
            catch 
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0;
                return new List<HangHoa>();
            }
        }

        public static bool Delete(string MaHangHoa)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("HangHoa_Xoa",
                MaHangHoa);
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool UpdatePromo(string maHangHoa, bool promo)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery(
                "HangHoa_SuaTrangThai", maHangHoa, promo);
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }

        public static List<HangHoa> Paging(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader(
                "HangHoa_TheoTrangAdmin",
                page,
                GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) /
                (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<HangHoa>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<HangHoa>();
            }
        }

        //public static object Paging(string page, out int howManyPages)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
